# -*- coding : utf_8 -*-
"""
=======================================
Time : 2025/4/14 10:45
Author : 田霄汉
Email : 522989570@qq.com
File : combat_class18.py
Project : pandas_demo
Function : 插入、修改、新增、删除数据行
=======================================
"""
import pandas as pd
from pandas import DataFrame
from config.setting import file_path

pd.options.display.max_columns = 999
combat24_path = file_path['excel_path'] + '/combat24.xlsx'
page01: DataFrame = pd.read_excel(io=combat24_path,
                                  sheet_name='page1',
                                  skiprows=3,
                                  usecols='C:E')
page02: DataFrame = pd.read_excel(io=combat24_path,
                                  sheet_name='page2',
                                  skiprows=3,
                                  usecols='C:E')
# 1、将page2的内容粘贴到page1，reset_index就是重新设置index，drop=True就是将旧的index删掉
students = page01.append(page02).reset_index(drop=True)
# 2、在最后一行插入series
stu01 = pd.Series({'id': 21, 'Full Name': '田霄汉', 'Test01': '120'})
students = students.append(stu01, ignore_index=True)
# 3、变更倒数第二行的数据内容
# # 第一种方法，不改变原行内存，只改变数据。
# students.at[19, 'Full Name'] = 'Baily Kate'
# students.at[19, 'Test01'] = 110
# 第二种方法，将原行直接替换，改变了原行内存
stu02 = pd.Series({'id': 20, 'Full Name': '叶月园', 'Test01': '110'})
students.iloc[19] = stu02
# 4、在中间插入一行数据
part01 = students[:10]
part02 = students[10:]
stu03 = pd.Series({'id': 100, 'Full Name': '田梓睿', 'Test01': '100'})
students: DataFrame = part01.append(stu03, ignore_index=True).append(part02, ignore_index=True) \
    .reset_index(drop=True)
# 5、按照序列进行删除
# students = students.drop(index=[0,1,2])
# students = students.drop(index=range(3))
# 按照切片进行删除
students = students.drop(index=students[0:3].index)
# 6、删除名字为空的数据行
# 首先把第5-7行的名字设置为空
for i in range(5, 8):
    students.at[i, 'Full Name'] = ''
missing = students.loc[students['Full Name'] == '']
students = students.drop(index=missing.index).reset_index(drop=True)
print(students)
